//
// Created by Administrator on 25-8-13.
//

#ifndef NTP_H
#define NTP_H
#include "main.h"
// --- 使用宏定义消除魔法数字 ---
#define NTP_SERVER_NAME         "ntp.aliyun.com"
#define NTP_SERVER_PORT_STR     "123"
// NTP 时间戳从 1900年1月1日 开始，而 Unix 时间戳从 1970年1月1日 开始
// 它们之间相差 2208988800 秒
#define NTP_TIMESTAMP_DELTA     2208988800UL
#define BEIJING_TIMEZONE_OFFSET (8 * 3600)
#define NTP_LI_VN_MODE          (0x23)

// NTP 报文结构体
typedef struct {
    uint8_t li_vn_mode;      // 8 bits. li, vn, and mode.
    // li: 2 bits. Leap indicator.
    // vn: 3 bits. Version number of the protocol (3 or 4).
    // mode: 3 bits. Mode. (3 = client)

    uint8_t stratum;         // 8 bits. Stratum level of the local clock.
    uint8_t poll;            // 8 bits. Maximum interval between successive messages.
    uint8_t precision;       // 8 bits. Precision of the local clock.

    uint32_t root_delay;     // 32 bits. Total round trip delay to the primary reference source.
    uint32_t root_dispersion; // 32 bits. Total dispersion to the primary reference source.
    uint32_t ref_id;         // 32 bits. Reference clock identifier.

    uint32_t ref_ts_secs;    // 32 bits. Reference timestamp seconds.
    uint32_t ref_ts_frac;    // 32 bits. Reference timestamp fraction of a second.

    uint32_t orig_ts_secs;   // 32 bits. Originate timestamp seconds.
    uint32_t orig_ts_frac;   // 32 bits. Originate timestamp fraction of a second.

    uint32_t rx_ts_secs;     // 32 bits. Receive timestamp seconds.
    uint32_t rx_ts_frac;     // 32 bits. Receive timestamp fraction of a second.

    uint32_t tx_ts_secs;     // 32 bits. Transmit timestamp seconds. 我们需要的就是这个！
    uint32_t tx_ts_frac;     // 32 bits. Transmit timestamp fraction of a second.
} ntp_packet;
void ntp_client_task(void* pvParams);
#endif //NTP_H
